package com.eshop.modules.mp.service.impl;

import org.slf4j.LoggerFactory;
import java.lang.invoke.SerializedLambda;
import java.io.IOException;
import java.util.Iterator;
import com.eshop.utils.FileUtil;
import java.util.ArrayList;
import javax.servlet.http.HttpServletResponse;
import com.eshop.common.utils.QueryHelpPlus;
import com.eshop.modules.mp.service.dto.ArticleDto;
import java.util.LinkedHashMap;
import com.github.pagehelper.PageInfo;
import java.util.Map;
import org.springframework.data.domain.Pageable;
import com.eshop.modules.mp.service.dto.ArticleQueryCriteria;
import java.io.Serializable;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eshop.modules.mp.vo.YxArticleQueryVo;
import java.util.List;
import org.springframework.beans.factory.annotation.Value;
import com.eshop.dozer.service.IGenerator;
import org.slf4j.Logger;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;
import com.eshop.modules.mp.service.ArticleService;
import com.eshop.modules.mp.domain.Article;
import com.eshop.modules.mp.service.mapper.ArticleMapper;
import com.eshop.common.service.impl.BaseServiceImpl;

@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = { Exception.class })
public class ArticleServiceImpl extends BaseServiceImpl<ArticleMapper, Article> implements ArticleService
{
    private static final Logger log;
    private final IGenerator generator;
    private final ArticleMapper articleMapper;
    @Value("${file.path}")
    private String uploadDirStr;
    
    public ArticleServiceImpl(final IGenerator generator, final ArticleMapper articleMapper) {
        this.generator = generator;
        this.articleMapper = articleMapper;
    }
    
    public List<YxArticleQueryVo> getList(final int page, final int limit) {
        final Page<Article> pageModel = (Page<Article>)new Page((long)page, (long)limit);
        final IPage<Article> pageList = (IPage<Article>)this.articleMapper.selectPage((IPage)pageModel, Wrappers.<Article>lambdaQuery().orderByDesc(Article::getId));
        return (List<YxArticleQueryVo>)this.generator.convert(pageList.getRecords(), (Class)YxArticleQueryVo.class);
    }
    
    public YxArticleQueryVo getDetail(final int id) {
        return (YxArticleQueryVo)this.generator.convert(this.getById((Serializable)id), (Class)YxArticleQueryVo.class);
    }
    
    public void incVisitNum(final int id) {
        this.articleMapper.incVisitNum(id);
    }
    
    public Map<String, Object> queryAll(final ArticleQueryCriteria criteria, final Pageable pageable) {
        this.getPage(pageable);
        final PageInfo<Article> page = (PageInfo<Article>)new PageInfo((List)this.queryAll(criteria));
        final Map<String, Object> map = new LinkedHashMap<String, Object>(2);
        map.put("content", this.generator.convert(page.getList(), (Class)ArticleDto.class));
        map.put("totalElements", page.getTotal());
        return map;
    }
    
    public List<Article> queryAll(final ArticleQueryCriteria criteria) {
        return (List<Article>)((ArticleMapper)this.baseMapper).selectList((Wrapper)QueryHelpPlus.getPredicate(Article.class, criteria));
    }
    
    public void download(final List<ArticleDto> all, final HttpServletResponse response) throws IOException {
        final List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        for (final ArticleDto yxArticle : all) {
            final Map<String, Object> map = new LinkedHashMap<String, Object>();
            map.put("分类id", yxArticle.getCid());
            map.put("文章标题", yxArticle.getTitle());
            map.put("文章作者", yxArticle.getAuthor());
            map.put("文章图片", yxArticle.getImageInput());
            map.put("文章简介", yxArticle.getSynopsis());
            map.put(" content", yxArticle.getContent());
            map.put("文章分享标题", yxArticle.getShareTitle());
            map.put("文章分享简介", yxArticle.getShareSynopsis());
            map.put("浏览次数", yxArticle.getVisit());
            map.put("排序", yxArticle.getSort());
            map.put("原文链接", yxArticle.getUrl());
            map.put("状态", yxArticle.getStatus());
            map.put("是否隐藏", yxArticle.getHide());
            map.put("管理员id", yxArticle.getAdminId());
            map.put("商户id", yxArticle.getMerId());
            map.put("产品关联id", yxArticle.getProductId());
            map.put("是否热门(小程序)", yxArticle.getIsHot());
            map.put("是否轮播图(小程序)", yxArticle.getIsBanner());
            list.add(map);
        }
        FileUtil.downloadExcel((List)list, response);
    }
    
    static {
        log = LoggerFactory.getLogger((Class)ArticleServiceImpl.class);
    }
}
